package app.tvprogram;

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class TvProgramConfigPanel extends JDialog {
    private JPanel jPanel;
    private JPanel jPanel1;
    private JScrollPane jScrollPane1;
    private JScrollPane jScrollPane2;
    private JList jList1;
    private JList jList2;
    private JPanel jPanel11;
    private JButton jButtonAdd;
    private JButton jButtonRemove;

    private JPanel jPanel2;
    private JButton jButtonOk;
    private JButton jButtonCancel;

    private JPanel jPanel3;
    private JLabel jLabel1;

    public TvProgramConfigPanel(Frame owner) {
        super(owner, true);
        initialize();
    }

    private void initialize() {
        jPanel = new JPanel();
        BorderLayout layout = new BorderLayout();
        this.setLayout(layout);
        this.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        jPanel1 = new JPanel();
        this.add(jPanel1);
        BorderLayout jPanel1Layout = new BorderLayout();
        jPanel1.setLayout(jPanel1Layout);
        {
            jScrollPane1 = new JScrollPane();
            jPanel1.add(jScrollPane1, BorderLayout.WEST);
            DefaultListModel dlm1 = new DefaultListModel();
            Map<Integer, String> dlm1DataMap = TvProgramManager.getInstance()
                    .getAvailableTvChannelMap();
            for (Integer key : dlm1DataMap.keySet()) {
                TvChannel tc = new TvChannel();
                tc.id = key;
                tc.name = dlm1DataMap.get(key);
                dlm1.addElement(tc);
            }
            jList1 = new JList(dlm1);
            jScrollPane1.setViewportView(jList1);
            jScrollPane2 = new JScrollPane();
            jPanel1.add(jScrollPane2, BorderLayout.EAST);
            DefaultListModel dlm2 = new DefaultListModel();
            Map<Integer, String> dlm2DataMap = TvProgramManager.getInstance()
                    .getFavoriteTvChannelMap();
            for (Integer key : dlm2DataMap.keySet()) {
                TvChannel tc = new TvChannel();
                tc.id = key;
                tc.name = dlm2DataMap.get(key);
                dlm2.addElement(tc);
            }
            jList2 = new JList(dlm2);
            jScrollPane2.setViewportView(jList2);
            jPanel11 = new JPanel();
            jPanel1.add(jPanel11, BorderLayout.CENTER);
            jButtonAdd = new JButton();
            jButtonAdd.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent evt) {
                    jButtonAddMouseClicked(evt);
                }
            });
            jButtonAdd.setText(">");
            jPanel11.add(jButtonAdd, BorderLayout.NORTH);
            jButtonRemove = new JButton();
            jButtonRemove.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent evt) {
                    jButtonRemoveMouseClicked(evt);
                }
            });
            jButtonRemove.setText("<");
            jPanel11.add(jButtonRemove, BorderLayout.SOUTH);
        }
        {
            jPanel2 = new JPanel();
            this.add(jPanel2, BorderLayout.SOUTH);
            jButtonOk = new JButton();
            jButtonOk.setText(Constants.label_operationOk);
            jButtonOk.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent evt) {
                    jButtonOkMouseClicked(evt);
                }
            });
            jPanel2.add(jButtonOk);
            jButtonCancel = new JButton();
            jPanel2.add(jButtonCancel);
            jButtonCancel.setText(Constants.label_operationCancel);
            jButtonCancel.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent evt) {
                    jButtonCancelMouseClicked(evt);
                }
            });
        }
        {
            jPanel3 = new JPanel();
            jLabel1 = new JLabel();
            jPanel3.add(jLabel1);
            jLabel1.setText(Constants.label_operationConfig);
            this.add(jPanel3, BorderLayout.NORTH);
        }
    }

    private void jButtonAddMouseClicked(MouseEvent evt) {
        System.out.println("jButtonAdd.mouseClicked, event=" + evt);
        Object obj = this.jList1.getSelectedValue();
        if (obj != null) {
            DefaultListModel dlm1 = (DefaultListModel) this.jList1.getModel();
            dlm1.remove(this.jList1.getSelectedIndex());
            DefaultListModel dlm2 = (DefaultListModel) this.jList2.getModel();
            dlm2.addElement(obj);
        }
    }

    private void jButtonRemoveMouseClicked(MouseEvent evt) {
        System.out.println("jButtonRemove.mouseClicked, event=" + evt);
        Object obj = this.jList2.getSelectedValue();
        if (obj != null) {
            DefaultListModel dlm2 = (DefaultListModel) this.jList2.getModel();
            dlm2.remove(this.jList2.getSelectedIndex());
            DefaultListModel dlm1 = (DefaultListModel) this.jList1.getModel();
            dlm1.addElement(obj);
        }
    }

    private void jButtonOkMouseClicked(MouseEvent evt) {
        System.out.println("jButtonOk.mouseClicked, event=" + evt);
        DefaultListModel dlm2 = (DefaultListModel) this.jList2.getModel();
        if (dlm2.size() > 0) {
            Set<Integer> idSet = new HashSet<Integer>();
            for (int i = 0; i < dlm2.size(); i++) {
                TvChannel tc = (TvChannel) dlm2.get(i);
                idSet.add(tc.id);
            }
            TvProgramManager.getInstance().setUserFavoriteTvChannelIdSet(idSet);
        }
        this.setVisible(false);
    }

    private void jButtonCancelMouseClicked(MouseEvent evt) {
        System.out.println("jButtonCancel.mouseClicked, event=" + evt);
        this.setVisible(false);
    }
}
